//
//  MainTabBarController.swift
//  新浪微博
//
//  Created by 李赛 on 15/5/9.
//  Copyright (c) 2015年 李赛. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
   
    @IBOutlet weak var mainTbaBar: MainTabBar!
     
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        // 设置按钮的监听事件   直接添加Target就可以了
        mainTbaBar.composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    }


    /**
    抽取出公用的方法
    */
    private func addChildViewController (name:String, title:String, imgName:String){
        
        let sb = UIStoryboard(name: name, bundle: nil)
        let nav = sb.instantiateInitialViewController() as! UINavigationController
        
        // 设置navigationController的属性
//        nav.title = title
        nav.topViewController.title = title
        nav.tabBarItem.image = UIImage(named: imgName)
        nav.tabBarItem.selectedImage = UIImage(named: imgName + "_highlighted")
        
        addChildViewController(nav)
    }
    
    // 添加子控件
    private func addChildViewControllers(){
        self.tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController("Home", title: "主页", imgName: "tabbar_home")
        addChildViewController("Message", title: "消息", imgName: "tabbar_message_center")
        addChildViewController("Discover", title: "发现", imgName: "tabbar_discover")
        addChildViewController("Profile", title: "我", imgName: "tabbar_profile")
    }
    
    func composeButtonClick(){
        
        // 判断用户是否登录
        let name = sharedUserAccount?.access_token == nil ? "OAuth" : "Compose"
        
        presentViewController(UIStoryboard.initialViewController(name), animated: true, completion: nil)
    }
    
}
